package com.umanji.category;

import java.util.Collections;
import java.util.UUID;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.umanji.category.domain.Categories;
import com.umanji.category.service.CategoryService;
import com.umanji.user.UserController;

/**
 * Handles requests for the application welcome page.
 */
@Controller
@RequestMapping(value="/category")
public class CategoryController {

	private Logger logger = org.slf4j.LoggerFactory.getLogger(CategoryController.class);

	private Validator validator;

    @Autowired
    private CategoryService categoryService;

	@Autowired
	public CategoryController(Validator validator) {
		this.validator = validator;
	}

    @RequestMapping(value="/children", method=RequestMethod.GET)
    public @ResponseBody List<Categories> getAvailability(@RequestParam Long id) {
    	Categories category = categoryService.find(id);
        return category.getChildren();
    }

	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public ModelAndView newCategoryForm() {
		logger.info("register category form!");

		Categories umanji_category = new Categories( );
        ModelAndView mav = new ModelAndView();
        mav.setViewName("category/createForm");
        mav.addObject("umanji_category", umanji_category);
        mav.addObject("umj_root_category", categoryService.findRootCategories());
        return mav;

	}

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String create(@ModelAttribute("umanji_category") Categories umanji_category,
						@RequestParam("parent_id") Long parent_id, BindingResult result) {
		logger.info("register user!");
		
		logger.info("parent id : " + parent_id);
		logger.info("name : " + umanji_category.getCategoryName());
		logger.info("code : " + umanji_category.getCategoryCode());
		logger.info("code : " + umanji_category.getCategoryOrder());

        for (ConstraintViolation<Categories> constraint : validator.validate(umanji_category)) {
            result.rejectValue(constraint.getPropertyPath().toString(), "", constraint.getMessage());
            logger.info("error msg : " + result.getAllErrors().toString());
        }
        logger.info("error report : " + result.getAllErrors());
        if (result.hasErrors())
        	return "/category/createForm";

        return "redirect:/umj/category/create";
	}

	protected void loadInitData() {
		Categories umanji_init_category = new Categories("중고거래", "usedMarket", 0);
		categoryService.persist(umanji_init_category);
		umanji_init_category = new Categories("알바", "job", 1);
		categoryService.persist(umanji_init_category);
		umanji_init_category = new Categories("발자취", "trace", 2);
		categoryService.persist(umanji_init_category);
	}

	// internal helpers
	
	private Map<String, String> validationMessages(Set<ConstraintViolation<Categories>> failures) {
		Map<String, String> failureMessages = new HashMap<String, String>();
		for (ConstraintViolation<Categories> failure : failures) {
			failureMessages.put(failure.getPropertyPath().toString(), failure.getMessage());
		}
		return failureMessages;
	}
}
